/**
 * Copyright 2010 Creative Works, Inc.
 * Creative Works licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.sendvox.freswitch.core;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.sendvox.freswitch.util.Arguments;

public final class NetAddress {

	private String host;
	private String password;
	private int port;

	private static final int DEFAULT_FREESWITCH_INBOUND_PORT = 8021;

	public NetAddress(Integer port) throws ConnectionException {
		try {
			this.host = InetAddress.getLocalHost().getHostAddress();
			Arguments.INSTANCE.validate(port).isNotNull().isNotEmpty()
					.isInRange(0, 0xFFFF);
			Arguments.INSTANCE.validate(host).isValidHostAddress();
		} catch (UnknownHostException e) {
			throw new ConnectionException(e, host);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		this.port = port;
	}

	public NetAddress(String host, Integer port) throws ConnectionException {
		try {
			Arguments.INSTANCE.validate(port).isNotNull().isNotEmpty()
					.isInRange(0, 0xFFFF);
			Arguments.INSTANCE.validate(host).isValidHostAddress();
		} catch (UnknownHostException e) {
			throw new ConnectionException(e, host);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		this.host = host;
		this.port = port;
	}

	/**
	 * *
	 * <p>
	 * A valid port value is between 0 and 65535. A port number of
	 * <code>zero</code> will let the system pick up an ephemeral port in a
	 * <code>bind</code> operation.
	 * <P>
	 * 
	 * @param hostName
	 *            the Host name
	 * @param port
	 *            The port number
	 * @param password
	 * @param password
	 * @param timeoutSeconds
	 * @throws IllegalArgumentException
	 *             if the port parameter is outside the range of valid port
	 *             values, or if the hostname parameter is <TT>null</TT>.
	 * 
	 */
	public NetAddress(String host, Integer port, String password)
			throws ConnectionException {
		try {
			Arguments.INSTANCE.validate(host, password).isNotNull()
					.isNotEmpty().isInRange(0, 0xFFFF);
			Arguments.INSTANCE.validate(host).isValidHostAddress();
		} catch (UnknownHostException e) {
			throw new ConnectionException(e, host);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		this.host = host;
		this.port = port;
		this.password = password;
	}

	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public String getPassword() {
		return password;
	}

	public static int getDefaultPort() {
		return DEFAULT_FREESWITCH_INBOUND_PORT;
	}

	@Override
	public String toString() {
		return "server [hostName=" + host + ", port=" + port + "]";
	}

}
